﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using DataAccessLayer;

namespace PresentationLayerWebApp
{
    public partial class WebFormLogin : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if ((Request.Cookies["UserName"] == null) && (Request.Cookies["Pwd"] == null))
            { }
            else
            {
                Login.UserName = Request.Cookies["UserName"].Value;
            }
        }

        protected void Login_Authenticate(object sender, AuthenticateEventArgs e)
        {
            if (DataAccessManager.isEnabled(Login.UserName) == false)
                Login.FailureText = "Your account has been disabled";
            else
            {
                if (DataAccessManager.ValidateUser(Login.UserName, Login.Password))
                {
                    Session["currentUser"] = Login.UserName;
                    Session["LastLoginDate"] = DataAccessManager.GetVersion(Login.UserName);
                    e.Authenticated = true;

                    if (Login.RememberMeSet == true)
                    {
                        HttpCookie myNameCookie = new HttpCookie("UserName");
                        myNameCookie.Value = Login.UserName;
                        Response.Cookies.Add(myNameCookie);
                    }

                }
                else
                {
                    if (DataAccessManager.isEnabled(Login.UserName) == false)
                        Login.FailureText = "Your account has been disabled";
                    e.Authenticated = false;
                }
            }
        }
    }

   

}